Ext.define('resources.controller.RegisterMainDashboard', {
    extend: 'Ext.app.Controller',
    
	models: [
		'SysMenuResource',
		'SysUrlResource',
		'RegisterModuleSel'
		],
	
    stores: [
		'RegisterModuleStore',
		'RegisterModuleSelStore',
		'SysMenuResourceStore',
		'SysUrlResourceStore'
	],
    
    views: [
    	'RegisterMainDashboard',
    	'RegisterMenuPanel',
		'RegisterUrlPanel',
		'RegisterModuleTreePanel'
    ],

    refs: [
		{ref: 'detailResourceTab',selector:'tabpanel'},
		{ref: 'sfModuleSelect',selector:'regmenupanel combobox[name=moduleSel]'},
		{ref: 'inputMenuName',selector:'regmenupanel textfield[name=menuname]'},
		{ref: 'regmenupanel',selector:'regmenupanel'},
		{ref: 'regurlpanel',selector:'regurlpanel'}
	],

    init: function() {

    	this.control({

			'regmaindashboard button[action=refleshcache]': {
			    click:this.refleshcache
			},

            'regmenupanel button[action=register]': {
                click: this.registerMenu
            },
            
            'regmenupanel button[action=unregister]': {
                click: this.unregisterMenu
            },
            // --- this component need store object 
            'regmenupanel button[action=menusimplesearch]': {
                click:this.searchMenuBySimpleName
            },
			
			'regmenupanel combobox[name=moduleSel]': {
				select: this.searchBySelectModule
			},
			
			'registertreepanel' : {
				itemclick : this.selectModule
			},
			
			'regurlpanel button[action=register]' : {
				click: this.registerUrl
			}
			
            
    	});
    },
	
	refleshcache:function () {
		// --- update record using ajax
		try {
			
			Ext.Ajax.request({
				url: '../data/resource/refleshcache.act',
				success: function(response, opts) {
					 
					 var obj = Ext.decode(response.responseText);
					 
					 // --- popup message ---
					Ext.MessageBox.show({
        				title: "提示",
        				msg: obj.msg,
        				buttons: Ext.MessageBox.OK,
        				icon: Ext.MessageBox.WARNING
       				});

				},
				failure: function(response, opts) {
				
				}
			});
			
		} catch (e) {
			alert(e);
		}
	},
	
	searchBySelectModule:function(field, value ) {

	},
	
	// --- defined this controller event ---
	selectModule:function (me, record, item, index, e) {
		var isLeaf = record.isLeaf();
		if (!isLeaf) {
			return ;
		}
		
		// --- get active panel ---
		var tabPanel = this.getDetailResourceTab();
		
		var activityPanel = tabPanel.getActiveTab();
		
		// --- add proxy to parameter --
		
		// --- reflesh panel store ---
		if ( activityPanel.refleshData ) {
			activityPanel.refleshData({
				'moduleId':record.get('id')
			});
		}
	},
	
	// ---- menu resource panel search filed ---
	searchMenuBySimpleName:function() {
		// --- find and search file ---
		var moduleSel = this.getSfModuleSelect().getValue();
		var inputMenuName = this.getInputMenuName().getValue();
		
		// ---binding object to proxy ---
		var store = this.getRegmenupanel().getStore();
		var proxy  = store.getProxy();
		if ( moduleSel ) {
			proxy.extraParams['moduleSel'] = moduleSel;
		}
		if (inputMenuName) {
			proxy.extraParams['menuname'] = inputMenuName;
		}
        store.load();

	},
	
	registerMenu:function() {
		
		// --- select module ---
		var selMod = this.getRegmenupanel().getSelectionModel();
		var records = selMod.getSelection();
		
		var jsonObjs = [];
		if (records.length) {
			for (var i = 0 ; i < records.length ; i++) {
				var obj = {
					code : records[i].get('code'),
					name : records[i].get('name')
				}
				jsonObjs.push( obj );				
			}
		}
		
		// --- update record using ajax
		try {

			Ext.Ajax.request({
				url: '../data/resource/regmenu.act',
				method:'post',
				params:{
					'jsonData':Ext.encode(jsonObjs)
				},
				success: function(response, opts) {
					 
					 var obj = Ext.decode(response.responseText);
					 
					 // --- popup message ---
					Ext.MessageBox.show({
        				title: "提示",
        				msg: obj.msg,
        				buttons: Ext.MessageBox.OK,
        				icon: Ext.MessageBox.WARNING
       				});
					
					// --- reload ---

				},
				failure: function(response, opts) {
				
				}
			});

			
		} catch (e) {
			alert(e);
		}
	},
	
	unregisterMenu:function() {
		// --- update record using ajax
		try {
			
			Ext.Ajax.request({
				url: '../data/resource/unregmenu.act',
				method:'post',
				params:{
					'ids':'ggg'
				},
				success: function(response, opts) {
					 
					 var obj = Ext.decode(response.responseText);
					 
					 // --- popup message ---
					Ext.MessageBox.show({
        				title: "提示",
        				msg: obj.msg,
        				buttons: Ext.MessageBox.OK,
        				icon: Ext.MessageBox.WARNING
       				});

				},
				failure: function(response, opts) {
				
				}
			});
			
		} catch (e) {
			alert(e);
		}
		
	},
	
	registerUrl : function() {
		// --- select module ---
		var selMod = this.getRegurlpanel().getSelectionModel();
		var records = selMod.getSelection();
		
		var jsonObjs = [];
		if (records.length) {
			for (var i = 0 ; i < records.length ; i++) {
				var obj = {
					code : records[i].get('code'),
					link : records[i].get('link')
				}
				jsonObjs.push( obj );				
			}
		}
		
		// --- update record using ajax
		try {

			Ext.Ajax.request({
				url: '../data/resource/regurl.act',
				method:'post',
				params:{
					'jsonData':Ext.encode(jsonObjs)
				},
				success: function(response, opts) {
					 
					 var obj = Ext.decode(response.responseText);
					 
					 // --- popup message ---
					Ext.MessageBox.show({
        				title: "提示",
        				msg: obj.msg,
        				buttons: Ext.MessageBox.OK,
        				icon: Ext.MessageBox.WARNING
       				});
					
					// --- reload ---

				},
				failure: function(response, opts) {
				
				}
			});

			
		} catch (e) {
			alert(e);
		}
	}
	
	
});